<template>
  <work-template v-model="detailForm" @submitOn="submitOn" @closeSelectedTag="closeSelectedTag" :butShow="bool">
    <div slot="form" class="container-div">
      <!--基础信息-->
      <basic-info v-if="$route.query && $route.query.insuranceChannels != '2'"
                  v-model="detailForm" :isDownload="isDownload" />

      <hoji-offer-info v-if="$route.query && $route.query.insuranceChannels == '2'"
          v-model="detailForm" :isDownload="isDownload" />
      <!--交强险信息-->
      <compulsory-Traffic-Tnsurance v-if="[2,3].includes(detailForm.insuranceChannels)" v-model="detailForm" ref="refCompulsory"
        :state="isEdit" />
      <!--商业险信息-->
      <commercia-insurance v-if="[2,3].includes(detailForm.insuranceChannels)" v-model="detailForm" ref="refCommercia"
        :state="isEdit" />
      <!--投保要求-->
      <division-purchase v-if="detailForm.insuranceChannels == 1" v-model="detailForm" ref="refDivision"
        :state="isEdit" />
      <!--其他保险-->
      <otherPrice v-model="detailForm" :state="isEdit" ref="refOtherPrice"
             :switchBoole="true"
             v-if="[1].includes(detailForm.insuranceChannels)" />

      <other v-model="detailForm" :state="isEdit" ref="refOther"
             :switchBoole="true"
             v-if="[2,3].includes(detailForm.insuranceChannels)" />

      <!--批量(客户自购)-->
      <client-batch v-model="detailForm" v-if="$route.query && $route.query.insuranceChannels == '3'"
        ref="refClientBatch" :state="isEdit" />

      <!--批量（店保 ）-->
      <vehicle-entry v-model="detailForm" ref="refVehicleEntry"
        v-if="$route.query && $route.query.insuranceChannels == '2'" :state="isEdit" />
      <!--备注-->
      <remark v-if="detailForm.insuranceChannels == 2" ref="refRemark" v-model="detailForm" :state="isEdit" />
    </div>
  </work-template>

</template>

<script>
import { batchData } from "@/api/cwgd/cwgdApi";
import { newAddInsuranceList } from "@/api/cwgd/insureApi.js";
import { log } from "mathjs";

export default {
  dicts: ["car_business_type"],
  name: "InsuranceInquiryDetails",
  components: {
    basicInfo: () => import("../components/carWorkOrder/basicInfo.vue"),
    hojiOfferInfo:() => import("../components/carWorkOrder/hojiOfferInfo.vue"),
    compulsoryTrafficTnsurance: () =>
      import("../components/carWorkOrder/compulsoryTrafficTnsurance.vue"),
    commerciaInsurance: () =>
      import("../components/carWorkOrder/commerciaInsurance.vue"),
    divisionPurchase: () =>
      import("../components/carWorkOrder/divisionPurchase.vue"),
    other: () => import("../components/carWorkOrder/other.vue"),
    otherPrice: () => import("../components/carWorkOrder/otherPrice.vue"),
    clientBatch: () => import("../components/carWorkOrder/clientBatch.vue"),
    vehicleEntry: () => import("../components/carWorkOrder/vehicleEntry.vue"),
    remark: () => import("../components/carWorkOrder/remark.vue"),
  },
  watch: {},
  computed: {
    isEdit() {
      if (this.$route.query.type === "edit") {
        return "write";
      }
      if (this.$route.query.type === "view") {
        return "readonly";
      }
    },
    bool() {
      if (this.$route.query.type === "edit") {
        return true;
      }
      if (this.$route.query.type === "view") {
        return false;
      }
    },
    isDownload() {
      if (
        this.$route.query.type === "edit" &&
        (this.detailForm.insuranceChannels === 2 ||
          this.detailForm.insuranceChannels === 1)
      ) {
        return true;
      }
      if (this.$route.query.type === "view") {
        return false;
      }
    },
  },
  data() {
    return {
      detailForm: {
        //基础信息
        businessList: [],
        insuranceVoList: [{}],
        insuranceChannels: null,
      },
      boole: false,
    };
  },

  created() {
    if (typeof this.$route.query.id === "object") {
      this.handleData(this.$route.query.id);
    } else {
      this.handleData([this.$route.query.id]);
    }
  },
  mounted() {},
  methods: {
    handleData(id) {
      batchData(id).then((res) => {
        if (res.code === 200) {
          this.detailForm = res.data;
          this.detailForm.otherOpenStatus = res.data?.otherOpenStatus || 0
          if (!this.detailForm.syxInsuredName) {
            this.detailForm.syxInsuredName =
              this.$route.query.propertyRightName || "";

              this.detailForm.insuranceVoList.forEach((item) => {
            item.jqxInsuredName = item.syxInsuredName =
              this.$route.query.propertyRightName;
          });
          }
          if (!this.detailForm.jqxInsuredName) {
            this.detailForm.jqxInsuredName =
              this.$route.query.propertyRightName || "";
          }
          this.detailForm.workId = id[0]


          //this.handleTableData();
        }
      });
    },

    handleTableData() {
      let arr = this.detailForm.insuranceVoList;
      let list = [];
      for (let i = 0; i < arr.length; i++) {
        list = this.detailForm.businessList.map((a, index) => {
          return {
            insuranceProName: a.insuranceProName,
            insuranceQuota: a.insuranceQuota,
            insurancePrice: a.insurancePrice,
          };
        });
        this.$set(this.detailForm.insuranceVoList[i], "insuranceList", list);
      }
      let index = this.detailForm.insuranceVoList.findIndex(
        (a) => a.insuranceList.length > 0
      );
      if (index !== -1) {
        this.boole = true;
      }
    },

    randomString(len) {
      len = len || 32;
      let timestamp = new Date().getTime();
      /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
      let $chars = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
      let maxPos = $chars.length;
      let randomStr = "";
      for (let i = 0; i < len; i++) {
        randomStr += $chars.charAt(Math.floor(Math.random() * maxPos));
      }

      return randomStr + timestamp;
    },

    submitOn() {
      let refs;
      if (Number(this.$route.query.insuranceChannels) === 2) {
        let refCompulsory = this.$refs.refCompulsory.handleEffect();
        let refCommercia = this.$refs.refCommercia.handleEffect();
        let refVehicleEntry = this.$refs.refVehicleEntry.handleEffect();
        let refRemark = this.$refs.refRemark.handleEffect();
        let refOther = this.$refs.refOther.handleEffect();
        refs = [refCompulsory, refCommercia, refOther, refVehicleEntry,refRemark];
      }
      if (Number(this.$route.query.insuranceChannels) === 1) {
        let refDivision = this.$refs.refDivision.handleEffect();
        // let refVehicleEntry = this.$refs.refVehicleEntry.handleEffect();
        let refOtherPrice = this.$refs.refOtherPrice.handleEffect();
        refs = [refDivision, refOtherPrice];
      }
      if (Number(this.$route.query.insuranceChannels) === 3) {
        refs = [this.$refs.refClientBatch.handleEffect()];
      }
      Promise.all(refs)
        .then(() => {

          const loading = this.$loading({
            lock: true,
            text: "保存中",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)",
          });
          newAddInsuranceList(this.detailForm)
            .then((res) => {
              loading.close();
              this.closeSelectedTag();
            })
            .catch(() => {
              loading.close();
            });
        })
        .catch(() => {});
    },

    closeSelectedTag() {
      this.$tab.closePage().then(({ visitedViews }) => {
        this.$router.push({
          path: "/cwgd/newCarInsuranceList",
        });
      });
    },
  },
};
</script>


<style lang="scss" scoped>
@import "@/styles/cwgd/container.scss";
</style>
